package com.sgzn.sgpay.web.controller;

import com.sgzn.sgpay.model.request.*;
import com.sgzn.sgpay.model.request.util.PageQueryParam;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.model.response.result.PresentQueryPageResult;
import com.sgzn.sgpay.model.response.vo.MerchantDevicePresentVo;
import com.sgzn.sgpay.persistence.domain.TPresent;
import com.sgzn.sgpay.service.SgsPresentService;
import com.sgzn.sgpay.web.controller.base.MerchantBaseController;
import com.sgzn.sgpay.web.handle.AssembleCommonHandle;
import com.sgzn.sgpay.web.request.Impl.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 礼品管理控制器
 * @author huangsenguang
 * @version Id: PresentController.java, v 0.1 2018/5/1 11:08 huangsenguang Exp $$
 */
@Slf4j
@Controller
@RequestMapping("/merchant/present")
public class PresentController extends MerchantBaseController{
    @Resource
    private SgsPresentService sgsPresentService;
    @Resource
    private AssembleCommonHandle assembleCommonHandle;

    /**
     * 添加礼品管理
     * @param request
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public SgsResultResponse<String> addPresent(PresentAddRequest request) {
        super.setMchInfo(request);
        return this.sgsPresentService.addPresent(assembleAddRequest(request));
    }

    /**
     * 删除礼品管理
     * @param request
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public SgsResultResponse<String> deletePresent(PresentDeleteRequest request) {
        super.setMchInfo(request);
        return this.sgsPresentService.deletePresent(assembleDeleteRequest(request));
    }

    /**
     * 查询礼品管理的信息
     * @param request
     * @return
     */
    @RequestMapping("query")
    @ResponseBody
    public SgsResultResponse<TPresent> queryPresentInfo(PresentQueryByIdRequest request) {
        super.setMchInfo(request);
        return this.sgsPresentService.queryPresent(assembleQueryRequest(request));
    }

    /**
     * 分页查询礼品管理的信息
     * @param request
     * @return
     */
    @RequestMapping("queryPage")
    @ResponseBody
    public SgsResultResponse<PresentQueryPageResult> queryPagePresent(PresentQueryPageRequest request) {
        super.setMchInfo(request);
        return this.sgsPresentService.queryPage(assembleQueryPageRequest(request));
    }


    /**
     * 查询设备礼品列表及库存
     * @param request
     * @return
     */
    @RequestMapping("queryDevicePresentAndList")
    @ResponseBody
    public SgsResultResponse<MerchantDevicePresentVo> queryListAndStockPresent(PresentQueryListRequest request) {
        super.setMchInfo(request);
        SgsDevicePresentVoQuerytRequest sgsDevicePresentVoQuerytRequest = new SgsDevicePresentVoQuerytRequest();
        sgsDevicePresentVoQuerytRequest.setDeviceId(request.getDeviceId());
        assembleCommonHandle.assemble(request, sgsDevicePresentVoQuerytRequest);
        SgsResultResponse<MerchantDevicePresentVo> listSgsResultResponse =   sgsPresentService.queryDevicePresentVo(sgsDevicePresentVoQuerytRequest);
        return  listSgsResultResponse;
    }

    /**
     * 更新礼品管理信息
     * @param request
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public SgsResultResponse<String> updatePresent(PresentUpdateRequest request) {
        super.setMchInfo(request);
        return this.sgsPresentService.updatePresent(assembleUpdateRequest(request));
    }

    private SgsPresentAddRequest assembleAddRequest(PresentAddRequest request) {
        SgsPresentAddRequest sgsPresentAddRequest = new SgsPresentAddRequest();
        this.assembleCommonHandle.assemble(request, sgsPresentAddRequest);
        sgsPresentAddRequest.setPresentName(request.getPresentName());
        sgsPresentAddRequest.setPresentPrice(request.getPresentPrice());
        return sgsPresentAddRequest;
    }

    private SgsPresentDeleteRequest assembleDeleteRequest(PresentDeleteRequest request) {
        SgsPresentDeleteRequest sgsPresentDeleteRequest = new SgsPresentDeleteRequest();
        this.assembleCommonHandle.assemble(request, sgsPresentDeleteRequest);
        sgsPresentDeleteRequest.setPresentId(request.getPresentId());
        return sgsPresentDeleteRequest;
    }

    private SgsPresentQueryRequest assembleQueryRequest(PresentQueryByIdRequest request) {
        SgsPresentQueryRequest sgsPresentQueryRequest = new SgsPresentQueryRequest();
        this.assembleCommonHandle.assemble(request, sgsPresentQueryRequest);
        sgsPresentQueryRequest.setPresentId(request.getPresentId());
        return sgsPresentQueryRequest;
    }

    private SgsPresentUpdateRequest assembleUpdateRequest(PresentUpdateRequest request) {
        SgsPresentUpdateRequest sgsPresentRequest = new SgsPresentUpdateRequest();
        this.assembleCommonHandle.assemble(request, sgsPresentRequest);
        sgsPresentRequest.setPresentId(request.getPresentId());
        sgsPresentRequest.setPresentName(request.getPresentName());
        sgsPresentRequest.setPresentPrice(request.getPresentPrice());
        return sgsPresentRequest;
    }

    private SgsPresentQueryPageRequest assembleQueryPageRequest(PresentQueryPageRequest request) {
        SgsPresentQueryPageRequest sgsPresentQueryPageRequest = new SgsPresentQueryPageRequest();
        this.assembleCommonHandle.assemble(request, sgsPresentQueryPageRequest);
        sgsPresentQueryPageRequest.setPresentId(request.getPresentId());
        PageQueryParam pageQueryParam = new PageQueryParam();
        pageQueryParam.setPageSize(Integer.parseInt(request.getPageSize()));
        pageQueryParam.setCurrentPage(Integer.valueOf(request.getPageNum()));
        sgsPresentQueryPageRequest.setPageQueryParam(pageQueryParam);
        sgsPresentQueryPageRequest.setMchId(request.getMchId());
        return sgsPresentQueryPageRequest;
    }
}

